package com.zgh.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.sun.org.apache.bcel.internal.generic.FADD;
import com.zgh.domain.*;
import com.zgh.service.BbsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/bbs")
public class BbsController {
    @Autowired
    private BbsService bbsService;

    /**
     * 获取所有论坛
     * @return 论坛集合
     */
    @RequestMapping(value = "/getBbs/{order}/{provinceName}",method = RequestMethod.GET)
    public Result getBbs(@PathVariable String order,@PathVariable String provinceName) {
        List<Bbs> bbsList = bbsService.getAllBbsByTime(order,provinceName);
        return new Result(true,"查询成功",bbsList);
    }

    /**
     * 获取获取前三条（热门）
     * @return 论坛集合
     */
    @RequestMapping(value = "/getBbs/{provinceName}",method = RequestMethod.GET)
    public Result getBbs(@PathVariable String provinceName) {
        List<Bbs> bbsList = bbsService.getAllBbsByHot(provinceName);
        return new Result(true,"查询成功",bbsList);
    }

    /**
     * 根据id查询论坛
     * @param id 论坛id
     * @return Result
     */
    @RequestMapping(value = "/getBbsById/{id}",method = RequestMethod.GET)
    public Result getBbsById(@PathVariable String id){
        Bbs bbs = bbsService.findBbsById(id);
        //System.out.println(bbs);
        return new Result(true,"查询成功",bbs);
    }

    /**
     * 点赞
     * @param bbsId 论坛id
     * @param thumbCount 点赞数目
     * @return result
     */
    @RequestMapping(value = "/thumb/{bbsId}/{thumbCount}",method = RequestMethod.GET)
    public Result thumb(@PathVariable String bbsId,@PathVariable String thumbCount){
        bbsService.thumb(bbsId,thumbCount);
        return new Result(true,"操作成功",null);
    }

    @RequestMapping(value = "/getCollectCountByBbsId/{bbsId}",method = RequestMethod.GET)
    public Result getCollectCountByBbsId(@PathVariable String bbsId){
        int count = bbsService.getCollectCountByBbsId(bbsId);
        return new Result(true,"查询成功",count);
    }

    /**
     * 上传话题
     * @param bbs
     * @return 失败或成功
     */
    @RequestMapping(value = "/uploadBbs",method = RequestMethod.POST)
    public Result uploadBbs(@RequestBody String bbs){
        Bbs bbs1 = new Bbs();
        bbs1.setTitle(JSON.parseObject(bbs).getString("title"));
        bbs1.setContent(JSON.parseObject(bbs).getString("content"));
        bbs1.setCoverUrl(JSON.parseObject(bbs).getString("coverUrl"));
        bbs1.setProvinceName(JSON.parseObject(bbs).getString("region"));
        String openid = JSON.parseObject(bbs).getString("openid");
        User user1 = new User();
        user1.setOpenid(openid);
        bbs1.setUser(user1);
        int pd = bbsService.uploadBbs(bbs1);
        if (pd==1){
            return new Result(true,"发布成功",null);
        } else {
            return new Result(false,"发布失败",null);
        }
    }


    /**
     * 根据openid查询话题
     * @param
     * @return Result
     */
    @RequestMapping(value = "/getBbsByOpenid",method = RequestMethod.POST)
    public Result getBbsByOpenid(@RequestBody String openid){
        String openid1 = JSON.parseObject(openid).getString("openid");
        List <Bbs> bbsList = bbsService.findBbsByOpenid(openid1);
        //System.out.println(bbs);
        if (bbsList!=null){
            return new Result(true,"查询成功",bbsList);
        } else {
            return new Result(false,"查询失败",null);
        }
    }


    // 分页查询
    @RequestMapping(value = "/getAllBbsInfo",method = RequestMethod.POST)
    public Result getAllBbsInfo(@RequestBody String condition){
        //System.out.println(JSON.parseObject(condition));
        String query = JSON.parseObject(condition).getString("query");
        int pagenum = JSON.parseObject(condition).getInteger("pagenum");
        int pagesize = JSON.parseObject(condition).getInteger("pagesize");
        System.out.println(pagesize);

         ipageBbs ipageBbs = bbsService.getAllBbsInfo(pagenum,pagesize,query);
        //System.out.println(ipageUser);
        if (ipageBbs!=null){
            return new Result(true,"获取论坛列表成功",ipageBbs);
        }else {
            return new Result(false, "获取论坛列表失败", null);
        }
    }


    // 通过id 删除论坛信息
    @RequestMapping(value = "/deleteByid",method = RequestMethod.POST)
    public Result deleteByid(@RequestBody String idStr){
        Integer id = JSON.parseObject(idStr).getInteger("id");
        int count = bbsService.deleteByid(id);
        if (count!=0){
            return new Result(true,"删除成功",null);
        }else {
            return new Result(false, "删除失败", null);
        }
    }

    // 通过id获取论坛信息
    @RequestMapping(value = "/getInfoByid",method = RequestMethod.POST)
    public Result getUserInfoByid(@RequestBody String idStr){
        Integer id = JSON.parseObject(idStr).getInteger("id");
        Bbs bbs = bbsService.getBbsById(id);
        if (bbs!=null){
            return new Result(true,"获取成功",bbs);
        }else {
            return new Result(false, "获取失败", null);
        }
    }

    // 通过id修改论坛信息
    @RequestMapping(value = "/saveInfoByid",method = RequestMethod.POST)
    public Result saveInfoByid(@RequestBody String bbs){
        Bbs bbs1 = new Bbs();
        bbs1.setTitle(JSON.parseObject(bbs).getString("title"));
        bbs1.setProvinceName(JSON.parseObject(bbs).getString("provinceName"));
        bbs1.setAuthor(JSON.parseObject(bbs).getString("author"));
        bbs1.setId(JSON.parseObject(bbs).getInteger("id"));
        int count = bbsService.saveInfoByid(bbs1);
        if (count==1){
            return new Result(true,"保存论坛信息成功",null);
        }else {
            return new Result(false, "保存论坛信息失败", null);
        }
    }


    // 通过点赞量和搜索词获取话题
    @RequestMapping(value = "/getBbsByValue",method = RequestMethod.POST)
    public Result getBbsByValue(@RequestBody String value){
        String value1 = JSON.parseObject(value).getString("value");
        System.out.println(value1);
        List<Bbs> bbsList = bbsService.getBbsByValue(value1);
        if (bbsList!=null){
            return new Result(true,"获取话题信息成功",bbsList);
        }else {
            return new Result(false, "保存话题信息失败", null);
        }
    }


    @RequestMapping(value = "/getCountByUserId/{userId}",method = RequestMethod.GET)
    public Result getCountByUserId(@PathVariable String userId){
        int count = bbsService.getBlogCountById(userId);
        if (count!=0){
            return new Result(true,"获取论坛数量成功",count);
        }else {
            return new Result(false, "保存博客数量失败", null);
        }
    }

    /**
     * 根据userId查询博客
     * @param userId 用户id
     * @return Result
     */
    @RequestMapping(value = "/getBbsByUserId",method = RequestMethod.POST)
    public Result getBbsByUserId(@RequestBody String userId){
        String userId1 = JSON.parseObject(userId).getString("userId");
        List <Bbs> bbsList = bbsService.findBbsByUserId(userId1);
        //System.out.println(bbs);
        if (bbsList!=null){
            return new Result(true,"查询成功",bbsList);
        } else {
            return new Result(false,"查询失败",null);
        }
    }
}
